package com.example.controller;

import com.example.utils.*;
import com.example.utils.image.MingHanImageCompare;
import com.example.utils.image.RedisUtil;
import org.meowy.cqp.jcq.entity.CQImage;
import org.meowy.cqp.jcq.entity.CoolQ;
import org.meowy.cqp.jcq.message.CQCode;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author LEI
 * Created by LEI on 2020/4/17.
 */
public class ReplyController {

    public String addReply(String msg, Long fromGroup, Long fromQQ, CoolQ CQ){
        String [] arr = msg.split("\\s+");
        String key = "";
        StringBuilder reply = new StringBuilder();
        CQCode CC = CQCode.getInstance();
        if(arr.length >1&& StringUtils.isNotEmpty(arr[1])){
            key = arr[1];
        }
        if(arr.length >2&&StringUtils.isNotEmpty(arr[2])){
            for (int i = 2; i < arr.length; i++) {
                if(i>2)reply.append(" ");
                reply.append(arr[i]);
            }
        }
        if(StringUtils.isNotEmpty(key)&&StringUtils.isNotEmpty(reply.toString())){
            try {
                // 判断key是否有图片
                String image = StringUtils.getMsgFirstImage(key);
                String tempKey = key;
                // 添加图片回复
                if (StringUtils.isNotEmpty(image)) {
                    // 图片位置
                    String keyFilePath = Config.REPLY_IMAGE_KEY + "\\" + image;
                    // 将key中的图片保存到key目录下
                    File source = new File(keyFilePath);
                    CQImage cqImage = CC.getCQImage(msg);
                    byte[] bytes = cqImage.getBytes();
                    FileUtils.writeToLocal2(source, bytes);
                    tempKey = image;
                }

                // 判断消息是否有图片
                String replyFirstImage = StringUtils.getMsgFirstImage(reply.toString());
                String tempReplyMsg = reply.toString();
                // 图文回复
                if (StringUtils.isNotEmpty(replyFirstImage)) {
                    // 获取所有图片并存放到value目录下
                    List<CQImage> replyImages = CC.getCQImages(reply.toString());
                    // 获取所有图片名称
                    List<String> msgImages = StringUtils.getMsgImages(reply.toString(), -1);
                    for (int i = 0; i < replyImages.size(); i++) {
                        CQImage replyImage = replyImages.get(i);
                        File dest2 = new File(Config.REPLY_IMAGE_VALUE + "\\" + msgImages.get(i));
                        FileUtils.writeToLocal2(dest2, replyImage.getBytes());
                    }
                    //更新回复消息的图片位置
                    tempReplyMsg = StringUtils.replaceMsgImageAddress(reply.toString(), Config.REDIS_REPLY_IMAGE_VALUE + "/");
                }

                tempKey = tempKey.trim();

                // 图片作为key回复
                if (!key.equals(tempKey)) {
                    RedisUtil.setString(Config.REDIS_REPLY_IMAGE + tempKey, tempReplyMsg);
                    CQ.sendGroupMsg(fromGroup, CC.at(fromQQ) + DateUtils.getYYYYMMDDTime() + " 添加图片回复成功!");
                } else {
                    // 普通字符串回复
                    RedisUtil.setString(Config.REDIS_REPLY_STRING + tempKey, tempReplyMsg);
                    CQ.sendGroupMsg(fromGroup, CC.at(fromQQ) + DateUtils.getYYYYMMDDTime() + " 添加文字回复成功!");
                }
            }catch (IOException e) {
                e.printStackTrace();
                CQ.sendGroupMsg(fromGroup,CC.at(fromQQ)+" 添加消息遇到异常!");
            }
        }else {
            CQ.sendGroupMsg(fromGroup,CC.at(fromQQ)+" 添加消息格式错误! 添加回复  健  回复内容");
        }
        return "";
    }

    public String reply(String msg, Long fromGroup, Long fromQQ, CoolQ CQ){
        msg = msg.trim();
        // 1.判断msg是否有图片
        String image = StringUtils.getMsgFirstImage(msg);
        if(StringUtils.isNotEmpty(image)){
            try {
                CQCode CC = CQCode.getInstance();
                // 图片位置
                String keyFilePath = Config.REPLY_IMAGE_TEMP_KEY + "\\" + image;
                // 将key中的图片保存到key目录下
                File source = new File(keyFilePath);
                CQImage cqImage = CC.getCQImage(msg);
                byte[] bytes = cqImage.getBytes();
                FileUtils.writeToLocal2(source, bytes);

                // 直接通过key获取
                String string = RedisUtil.getString(Config.REDIS_REPLY_IMAGE+image);
                if(StringUtils.isNotEmpty(string)){
                    return string;
                }
                // 遍历比较本地图片是否有相似的
                List<String> fileNames = FileUtils.getFileNames(Config.REPLY_IMAGE_KEY);
                for (int i = 0; i < fileNames.size(); i++) {
                    String fileName = fileNames.get(i);
                    File file = new File(Config.REPLY_IMAGE_KEY + "\\" + fileName);
                    double similarity = MingHanImageCompare.getSimilarity(source, file);
                    if(similarity > 80){
                       String reMsg = RedisUtil.getString(Config.REDIS_REPLY_IMAGE+fileName);
                       if(StringUtils.isNotEmpty(reMsg)){
                           return reMsg;
                       }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            return RedisUtil.getString(Config.REDIS_REPLY_STRING+msg);
        }
        return null;
    }
}
